package com.dongdong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdong.data.DataResult;
import com.dongdong.data.ResponseStatusEnum;
import com.dongdong.pojo.TbBrand;
import com.dongdong.service.BrandService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Deacription TODO
 * @Author Lenovo
 * @Date 2020/12/9 19:33
 * @Version 1.0
 **/
@Controller
@RequestMapping("brandController")
public class BrandController {

    private static final Logger logger = LoggerFactory.getLogger(BrandController.class);

    @Reference
    private BrandService brandService;

    //跳转品牌管理页面
    @RequestMapping("list")
    public String list(){

        return "admin/brand";
    }

    //查询品牌
    @RequestMapping("brandList")
    @ResponseBody
    public DataResult brandList(){
        try {
            List<TbBrand> brandList = brandService.getBrandList();
            return DataResult.response(ResponseStatusEnum.SUCCESS).setData(brandList);
        }catch (Exception e){
            logger.error("查询失败",e);
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    //增加品牌
    @RequestMapping("saveBrand")
    @ResponseBody
    public DataResult saveBrand(TbBrand tbBrand){
        try {
            int i = brandService.saveBrand(tbBrand);
            return DataResult.response(ResponseStatusEnum.SUCCESS).setData(i);
        }catch (Exception e){
            logger.error("增加失败",e);
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    //跳转修改页面
    @RequestMapping("toBrandUpdate")
    @ResponseBody
    public DataResult toBrandUpdate(Long id){
        try {
            TbBrand tbBrand = brandService.toBrandUpdate(id);
            return DataResult.response(ResponseStatusEnum.SUCCESS).setData(tbBrand);
        }catch (Exception e){
            logger.error("回显失败",e);
            return DataResult.response(ResponseStatusEnum.SUCCESS);
        }
    }

    //修改品牌
    @RequestMapping("updateBrandInfo")
    @ResponseBody
    public DataResult updateBrandInfo(TbBrand tbBrand){
        try {
            int i = brandService.updateBrandInfo(tbBrand);
            return DataResult.response(ResponseStatusEnum.SUCCESS).setData(i);
        }catch (Exception e){
            logger.error("修改失败",e);
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("deleteBatch")
    @ResponseBody
    public DataResult deleteBatch(@RequestParam("ids[]") Long[] ids){
        try {
            int i = brandService.deleteBatch(ids);
            return DataResult.response(ResponseStatusEnum.SUCCESS).setData(i);
        }catch (Exception e){
            logger.error("删除失败");
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

}

